package client.audio;

import java.io.File;
import javax.media.Manager;
import javax.media.Player;


/**
 * @author primianoc
 *
 */
public class FileAudio
{
	/**
	 * Riferimento all'unica istanza del Singleton
	 */
	private static FileAudio _instance = null;

	private FileAudio()
	{

	}

	public static FileAudio getInstance()
	{
		if(_instance == null)
		{
			_instance = new FileAudio();
		}

		return _instance;
	}

	public int getDurationSeconds(File file)
	{
		int seconds = -1;

		try
		{
			Player player = Manager.createRealizedPlayer(file.toURI().toURL());
			seconds = (int)player.getDuration().getSeconds();
			player.close();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		} 

		return seconds;
	}

	public int getDurationMilliseconds(File file)
	{
		int milliSeconds = -1;

		try
		{
			Player player = Manager.createRealizedPlayer(file.toURI().toURL());
			milliSeconds = (int)(player.getDuration().getNanoseconds()/1000);
			player.close();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		} 

		return milliSeconds;
	}
}
